package cn.xuanxuanyuhua.crm.web.controller;

import cn.xuanxuanyuhua.crm.domain.Customer;
import cn.xuanxuanyuhua.crm.domain.Employee;
import cn.xuanxuanyuhua.crm.query.CustomerQueryObject;
import cn.xuanxuanyuhua.crm.query.QueryObject;
import cn.xuanxuanyuhua.crm.service.ICustomerService;
import cn.xuanxuanyuhua.crm.service.ISystemDictionaryService;
import cn.xuanxuanyuhua.crm.util.JSONResult;
import cn.xuanxuanyuhua.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService service;
    @Autowired
    private ISystemDictionaryService dictionaryService;

    @RequestMapping("/list")
    public String list(Model m,@ModelAttribute("qo")CustomerQueryObject qo){
        if (!UserContext.isAdminAndMgr()) {
            qo.setSellerId(UserContext.getActiveUser().getId());
        }
        qo.setOrderBy("c.inputTime desc");
        m.addAttribute("pageInfo",service.query(qo));
        m.addAttribute("jobs",dictionaryService.selectAllBySn("job"));
        m.addAttribute("sources",dictionaryService.selectAllBySn("source"));
        m.addAttribute("types",dictionaryService.selectAllBySn("communicationMethod"));
        return "/customer/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Customer entity){
        service.saveOrUpdate(entity);
        return new JSONResult();
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Object updateStatus(Customer entity){
        service.updateStatus(entity);
        return new JSONResult();
    }




}
